{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 基于线性回归的月度预算消费比例预测(多标签)\n",
    "import numpy as np\n",
    "import pandas\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from utils.mulanbay import get_datasetsPath"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:06.361981Z",
     "start_time": "2023-08-09T06:55:03.985416Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "#读取csv文件\n",
    "data_file = get_datasetsPath()+\"/\"+\"budget_consume_m_m.csv\"\n",
    "data_df = pandas.read_csv(data_file)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:06.409038Z",
     "start_time": "2023-08-09T06:55:06.365407Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 37572 entries, 0 to 37571\n",
      "Data columns (total 5 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   month     37572 non-null  int64  \n",
      " 1   score     37572 non-null  int64  \n",
      " 2   dayIndex  37572 non-null  int64  \n",
      " 3   rate1     37572 non-null  float64\n",
      " 4   rate2     37572 non-null  float64\n",
      "dtypes: float64(2), int64(3)\n",
      "memory usage: 1.4 MB\n"
     ]
    }
   ],
   "source": [
    "#查看数据集\n",
    "data_df.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:40.454895Z",
     "start_time": "2023-08-09T06:55:40.372931Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "#划分data和target\n",
    "data_X = data_df[[\"month\",\"score\",\"dayIndex\"]]\n",
    "data_y = data_df[[\"rate1\",\"rate2\"]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:46.721151Z",
     "start_time": "2023-08-09T06:55:46.715135Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "       month  score  dayIndex\n0          1      0         1\n1          1      0         2\n2          1      0         3\n3          1      0         4\n4          1      0         5\n...      ...    ...       ...\n37567     12    100        27\n37568     12    100        28\n37569     12    100        29\n37570     12    100        30\n37571     12    100        31\n\n[37572 rows x 3 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>month</th>\n      <th>score</th>\n      <th>dayIndex</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>0</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>0</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>37567</th>\n      <td>12</td>\n      <td>100</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>37568</th>\n      <td>12</td>\n      <td>100</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>37569</th>\n      <td>12</td>\n      <td>100</td>\n      <td>29</td>\n    </tr>\n    <tr>\n      <th>37570</th>\n      <td>12</td>\n      <td>100</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>37571</th>\n      <td>12</td>\n      <td>100</td>\n      <td>31</td>\n    </tr>\n  </tbody>\n</table>\n<p>37572 rows × 3 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_X"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:49.211642Z",
     "start_time": "2023-08-09T06:55:49.191150Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rate1\n",
      "rate2\n"
     ]
    }
   ],
   "source": [
    "#对于rate值特别大的，有时可能超过3\n",
    "print(min(data_y))\n",
    "print(max(data_y))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:52.100474Z",
     "start_time": "2023-08-09T06:55:52.090565Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "#通过随机森林回归来填补缺失值\n",
    "X_missing_reg = data_X.copy();"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:53.881031Z",
     "start_time": "2023-08-09T06:55:53.875947Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "array([], dtype=object)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算为空的列\n",
    "X_df = X_missing_reg.isnull().sum()\n",
    "# 得出列名 缺失值最少的列名 到 缺失值最多的列名\n",
    "colName = X_df[~X_df.isin([0])].sort_values().index.values\n",
    "colName"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:55:56.178913Z",
     "start_time": "2023-08-09T06:55:56.148269Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "from utils.mulanbay import fill_missing_rf\n",
    "#填补\n",
    "#选择标签的第一列为训练用的值\n",
    "fill_y = data_y.iloc[:,0]\n",
    "#使用列名进行索引填补\n",
    "for cn in colName:\n",
    "    y_pred = fill_missing_rf(X_missing_reg,fill_y,cn)\n",
    "    X_missing_reg.loc[X_missing_reg.loc[:,cn].isnull(),cn] = y_pred"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:56:40.724423Z",
     "start_time": "2023-08-09T06:56:40.710153Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "       month  score  dayIndex\n0          1      0         1\n1          1      0         2\n2          1      0         3\n3          1      0         4\n4          1      0         5\n...      ...    ...       ...\n37567     12    100        27\n37568     12    100        28\n37569     12    100        29\n37570     12    100        30\n37571     12    100        31\n\n[37572 rows x 3 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>month</th>\n      <th>score</th>\n      <th>dayIndex</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>0</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>0</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>37567</th>\n      <td>12</td>\n      <td>100</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>37568</th>\n      <td>12</td>\n      <td>100</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>37569</th>\n      <td>12</td>\n      <td>100</td>\n      <td>29</td>\n    </tr>\n    <tr>\n      <th>37570</th>\n      <td>12</td>\n      <td>100</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>37571</th>\n      <td>12</td>\n      <td>100</td>\n      <td>31</td>\n    </tr>\n  </tbody>\n</table>\n<p>37572 rows × 3 columns</p>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_missing_reg"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:56:42.668478Z",
     "start_time": "2023-08-09T06:56:42.638948Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 0, 0])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = np.sort(X_missing_reg.isnull().sum(axis=0))\n",
    "res"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:56:45.468289Z",
     "start_time": "2023-08-09T06:56:45.458548Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "-0.0010157274620471797"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#交叉验证\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.model_selection import cross_val_score\n",
    "reg = LinearRegression().fit(X_missing_reg, data_y)\n",
    "scores = cross_val_score(reg,X_missing_reg,data_y,scoring='neg_mean_squared_error',cv=5).mean()\n",
    "# NEG_MSE\n",
    "scores"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:56:49.947321Z",
     "start_time": "2023-08-09T06:56:49.828281Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "data_X = X_missing_reg"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T06:56:54.971504Z",
     "start_time": "2023-08-09T06:56:54.964665Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "#生成模型文件\n",
    "from sklearn_pandas import DataFrameMapper\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.feature_selection import SelectKBest\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn2pmml.decoration import ContinuousDomain\n",
    "from sklearn2pmml.pipeline import PMMLPipeline\n",
    "from sklearn.multioutput import MultiOutputRegressor\n",
    "\n",
    "#数据预处理\n",
    "mapper = DataFrameMapper([\n",
    "\t\t([\"month\", \"score\", \"dayIndex\"], [ContinuousDomain(), SimpleImputer()])\n",
    "\t    ])\n",
    "#算法\n",
    "#参考：https://scikit-learn.org.cn/view/91.html\n",
    "clf = MultiOutputRegressor(LinearRegression())\n",
    "\n",
    "pipeline = PMMLPipeline([\n",
    "\t(\"mapper\", mapper),\n",
    "\t(\"classifier\", clf)\n",
    "])\n",
    "\n",
    "pipeline.active_fields = [\"month\", \"score\", \"dayIndex\"]\n",
    "pipeline.target_fields = [\"rate1\",\"rate2\"]\n",
    "\n",
    "pipeline.fit(data_X, data_y)\n",
    "pipeline.verify(data_X.sample(n = 15))\n",
    "\n",
    "from sklearn2pmml import sklearn2pmml\n",
    "from utils.mulanbay import get_modulePath\n",
    "module_file = get_modulePath()+\"/\"+\"LinearRegressor_budget_consume_m_m.pmml\"\n",
    "sklearn2pmml(pipeline, module_file,with_repr = True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:12:22.488743Z",
     "start_time": "2023-08-09T03:12:18.795087Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
